
function onYouTubePlayerReady(playerId) {
}

function mediaType(t) {
  var mtypes = {
    'mp3': 'Audio',
    'gif': 'Photo',
    'jpg': 'Photo',
    'png': 'Photo',
    'youtube': 'Video',
  };
  if(t['link'].match('youtube') == 'youtube') {
    t['type'] = 'youtube';
  }else if(t['link'] == t['thumbnail']) {
    t['type'] = 'png';
  }

  try {
    mtype = mtypes[t['type']];
  }catch(e) {
    mtype = '';
  }
  if(mtype==undefined) {
    mtype = 'Photo';
  } 
  return mtype; 
}

function _best_width(img, width, height) {
  var bwidth = 0;
  if(width.indexOf('%') != -1) {
    width = window.document.getElementById(div).offsetWidth;
  }

  if(width >= height) {
    bwidth = height * img.width/img.height;
    bwidth = bwidth>width?width:bwidth;
  }else {
    if(img.height > img.width) {
      bwidth = width * width/height;
    }else{
      bwidth = width;
    }
  }
  return bwidth;
}

function embedYouTube(url, width, height, var_div) {
  var video_url = url.replace('watch?v=', 'v/')+'?autoplay=1&enablejsapi=1&version=3&playerapiid='+var_div;
  var embed = '<object width="'+pwidth+'" height="'+cheight+'"><param name="movie" ' +
             'value="'+video_url+'">'+
             '</param><param name="allowFullScreen" value="true"></param>'+
             '<param name="allowscriptaccess" value="always"></param><embed '+
             'src="'+video_url+'" '+
             'type="application/x-shockwave-flash" allowscriptaccess="always" '+
             'allowfullscreen="true" width="'+pwidth+'" height="'+cheight+'">'+
             '</embed></object>';
  return embed;
}


var preloaded_thumbnail = new Image();
preloaded_thumbnail.src = thumbnail;
var thumb_width = _best_width(preloaded_thumbnail, pwidth, cheight);
var chrome = '<div id="player-chrome" class="chrome"><img src="'+thumbnail+'" width="'+thumb_width+'px"/></div>';
var preloaded_images = new Array();
var controls = '<div class="controls"><ol>';
if(playlist.length>1) {
  chrome+= '<div id="player-caption">'+playlist.length+' items. <a href="#" onclick="showPlayer(0); return false;">Start</a> or click on another item in the playlist to enjoy it!</div>';
  for (k in playlist) {
    if (playlist[k]['title']!=null) {
      controls+= '<li class="'+(k%2?'':'even')+'"><a href="#" title="Click to learn more about this item" onclick="showPlayer('+k+'); return false;">'+ mediaType(playlist[k])+ ' - ' + playlist[k]['title'] + '</a></li>';
    }
    if(mediaType(playlist[k]) == 'Photo') {
      preloaded_images[k] = new Image();
      preloaded_images[k].src = playlist[k]['file'];
    }
  }
}else if(playlist.length ==1) {
  preloaded_images[0] = new Image();
  preloaded_images[0].src = playlist[0]['file'];
  chrome+= '<div id="player-caption"> <a href="#" onclick="showPlayer(0); return false;">Play '+mediaType(playlist[0])+' - '+ playlist[0]['title']+'</a></div>';
} 
chrome+= '</div>';
controls+= '</ol></div>';


function showPlayer(k) {
  var for_chrome = '';
  if(playlist[k]['link'].match('youtube') == 'youtube') {
    for_chrome+= embedYouTube(playlist[k]['file'], pwidth, cheight, div);
  }else if(playlist[k]['type'] == 'mp3') {
    //http://www.macloo.com/examples/audio_player/options.html
    for_chrome = '<img src="'+playlist[k]['thumbnail']+'" class="audio" height="'+(cheight-24)+'px" width="'+thumb_width+'px"/><br/>';
    for_chrome+= '<object type="application/x-shockwave-flash" '+
    'data="http://static.fienipa.com/sites/all/modules/kasahorow/kdictionary/kgallery/audio.swf" width="290" height="24" id="audioplayer1">'+
    '<param name="movie" value="http://static.fienipa.com/sites/all/modules/kasahorow/kdictionary/kgallery/audio.swf" />'+
    '<param name="FlashVars" value="playerID=1&amp;bg=0xdfe0d4&amp;'+
    'leftbg=0xa6a88b&amp;lefticon=0x0777a6&amp;rightbg=0xa6a88b&amp;'+
    'rightbghover=0x999999&amp;righticon=0x0777a6&amp;'+
    'righticonhover=0xffffff&amp;text=0x000000&amp;autostart=yes&amp;'+
    'slider=0x0777a6&amp;track=0xFFFFFF&amp;border=0x666666&amp;'+
    'loader=0x80c9ef&amp;soundFile='+playlist[k]['file']+'" />'+
    '<param name="quality" value="high" /><param name="menu" value="false" />'+
    '<param name="wmode" value="transparent" /></object>';
  }else {//must be an image
    bwidth = _best_width(preloaded_images[k], pwidth, cheight);
    for_chrome+= '<img src="'+preloaded_images[k].src+'" width="'+bwidth+'px"/>';
  }
  var iecaption = document.createElement('div');
  var caption_content = '<img src="'+playlist[k]['thumbnail']+'" '+
    'align="left" width="50px" height="50px"/><small>Now enjoying...</small><br/> '+ mediaType(playlist[k]) +
    ' <i>' +
    '<a href="'+playlist[k]['link']+'&utm_source=mplayer&utm_medium=js&utm_campaign=fPlayer">'+
    playlist[k]['title'] + '</a></i>'+'<br/>'+
    (k>0?' <a href="#" onclick="showPlayer('+(k-1)+'); return false;" title="Enjoy previous">&lt;Previous</a> |':'&nbsp;') +
    (k<playlist.length-1?' <a href="#" onclick="showPlayer('+(k+1)+'); return false;" title="Enjoy next">Next &gt;</a>':'');
  iecaption.innerHTML = caption_content;
  document.getElementById('player-caption').innerHTML = '';
  document.getElementById('player-caption').appendChild(iecaption);
  var iechrome = document.createElement('div');
  iechrome.innerHTML = for_chrome;
  document.getElementById('player-chrome').innerHTML = '';
  document.getElementById('player-chrome').appendChild(iechrome);
  return false;
}


var pdiv = window.document.getElementById(div);
var iediv = document.createElement('div');
iediv.innerHTML = chrome + controls;
try{
  pdiv.innerHTML = '';
  pdiv.appendChild(iediv);
}catch(e) {}

